import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;


public class Console extends JPanel  implements ActionListener,KeyListener  {
	
	private JTextArea console; 
	private boolean debutConsole = true;
	private Box box;
	private JTextField consoleExec;
	private JButton consoleExecBouton;
	private Logo logo;
	private String LastExec;
	
	public Console(Logo l){
		super();
		this.logo=l;
		this.setLayout(new BorderLayout());//adapte le JTextArea ? la fenetre de la console
		
		console = new JTextArea();
		console.setEditable(false);		
		box = Box.createHorizontalBox();
		consoleExec = new JTextField();
		consoleExec.addKeyListener(this);
		consoleExecBouton = new JButton("Executer");
		consoleExecBouton.addActionListener(this);
		
		this.add(new JScrollPane(console), BorderLayout.CENTER);
		this.add(box, BorderLayout.SOUTH);
		box.add(consoleExec);
		box.add(consoleExecBouton);	
	}
	
	public void out(String resultat)
	{	
		console.setForeground(Color.GREEN);
		if(debutConsole)
		{console.append(resultat);
		 debutConsole=false;
		}	
		else	
			console.append("\n"+resultat);
	}
	
	public void sys(String resultat)
	{
		console.setForeground(Color.BLUE);
		if(debutConsole)
		{console.append(resultat);
		 debutConsole=false;
		}	
		else

			console.append("\n"+resultat);
	}
	
	public void err(String resultat)
	{
		console.setForeground(Color.RED);
		if(debutConsole)
		{
			console.append(resultat);
			debutConsole=false;
		}	
		else			
			console.append("\n"+resultat);
	}
	
	public void ExecutionCode()
	{
		ArrayList<Object> instruction = new ArrayList<Object>();
    	ByteArrayInputStream stream  = new ByteArrayInputStream(consoleExec.getText().getBytes());	    	
    	Trans parser = new Trans(stream);
    	parser.initBibliotheque(logo.getBiblio());
    	Evaluation E = new Evaluation();
    	E.initEnv(new HashMap<String, Object>());    	
    	try {
				instruction = parser.run();
			} catch (ParseException e1) {
				e1.printStackTrace();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
        try {
				E.evalListe(instruction);
			} catch (IllegalArgumentException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			} catch (InvocationTargetException e1) {
				e1.printStackTrace();
			}
			LastExec=consoleExec.getText();
			consoleExec.setText("");
	}

	public void actionPerformed(ActionEvent e) 
	{				
		ExecutionCode();
	}
	
	public void keyPressed(KeyEvent e) 
	{
		int key = e.getKeyCode();
	    if (key == KeyEvent.VK_ENTER) 
	    {
	    	ExecutionCode();
	    }
	    if (key == 38) //38 = touche du haut
	    {
	    	consoleExec.setText(LastExec);
	    }
	}
	public void keyTyped(KeyEvent arg0) {}
	public void keyReleased(KeyEvent arg0) {}
}